from netrc import netrc
from operator import ne
from turtle import pensize


L = ['一', '二', '三', '四', '五', '六', '七', '八']

print(L)

r = []
n = 3
for i in range(3):
    r.append(L[i])

print(r)
print(L[0:3])
print(L[:3])
print(L[1:3])
print(L[-2:])
print(L[-2:-1])

L = list(range(100))
print(L)

print(L[:10])
print(L[-10:])
print(L[10:20])

print(L[:10:2])
print(L[::5])

print('abcdefg'[:3])
print('abcdefg'[::2])

for a in 'ABCDED':
    print(a)

for i, value in enumerate(['A', 'B', 'C']):
    print(i, value)

for i, value in [('A', 'a'), ('B', 'b'), ('C', 'c')]:
    print(i, value)

print([x for x in range(1, 11)])
print([x*x for x in range(1, 11)])
print([m+n for m in ['A', 'B', 'C'] for n in ['a', 'b', 'c']])

print([x*x for x in range(1, 11) if x % 2 == 0])
print([x if x % 2 == 0 else -x for x in range(1, 11)])

# generator
g = (x for x in range(1, 11))
print(g)
print(next(g))
print(next(g))

for n in g:
    print(n)


def fib(max):
    n, a, b = 0, 0, 1
    while (n < max):
        yield b
        a, b = b, a+b
        n += 1
    return 'ok'


# print(fib(6))
for n in fib(6):
    print(n)

b = fib(6)
while True:
    try:
        x = next(b)
        print(x)
        # 获取函数return值
    except StopIteration as e:
        print(e.value)
        break


def odd():
    print('1')
    yield 1
    print('2')
    yield 2
    print('3')
    yield 3


a = odd()
next(a)
next(a)
next(a)
next(odd())
next(odd())
next(odd())

print("杨辉三角")


def triangles():
    L = []
    for row in range(1, 7):
        if row < 3:
            rs = []
            for r in range(row):
                rs.append(1)
            L.append(rs)
            continue

        rs = []
        for r in range(row):
            if r == 0 or r == row-1:
                rs.append(1)
            else:
                rs.append(L[row-2][r-1]+L[row-2][r])
        L.append(rs)

    print(L)

# print(triangles())


def triangles2():
    L = []
    for row in range(1, 7):
        if row < 3:
            L.append([1 for r in range(row)])
            yield L[row-1]
        else:
            L.append([1 if r == 0 or r == row-1 else L[row-2][r-1]+L[row-2][r] for r in range(row)])
            yield L[row-1]

func = triangles2()
for f in func:
    print(f)

def triangles3():
    L = []
    while True:
        L.append(1)
        LL = [L[i-1]+L[i] if i>0 and i<len(L)-1 else 1 for i in range(0,len(L))]
        L = LL
        yield LL

func = triangles3()
print(next(func))
print(next(func))
print(next(func))
print(next(func))
print(next(func))
print(next(func))